package org.beetl.sql.springboot.dynamic;

import com.zaxxer.hikari.HikariDataSource;
import org.beetl.sql.core.ThreadLocalSQLManager;
import org.beetl.sql.springboot.conf.DynamicRoutingDataSource;


import java.io.InputStream;
import java.util.function.Function;

public class DynamicRoutingDataSourceAndSqlManager extends DynamicRoutingDataSource {
	@Override
	public void db(String dbName) {
		if (!checkExist(dbName)) {
			throw new IllegalArgumentException(dbName);
		}
		//同时切换数据源和sqlManager
		dbThreadLocal.set(dbName);
		String dbType = dbType(dbName);
		//每个数据库类型，如mysql，h2，oralce,对应一个sqlMaanger，参考配置 application-dynamic-ds-2.properties
		ThreadLocalSQLManager.locals.set("sqlManager"+dbType);
	}
	protected  String dbType(String dbName){
		//实际使用应该有个缓存，能告诉dbname是用的是哪一种数据库（SQLManager)
		if(dbName.startsWith("mysql")){
			return "Mysql";
		}else if(dbName.startsWith("h2")){
			return "H2";
		}else{
			throw new UnsupportedOperationException(dbName);
		}
	}

}
